package com.wuercloud.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wuercloud.entity.ConfigEntity;
import com.wuercloud.service.ConfigService;
import com.wuercloud.utils.PageUtils;
import com.wuercloud.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 配置管理
 * 
 * @author ChinaSun
 * @email hjsw1@126.com
 * @date 2017-03-21 16:23:30
 */
@RestController
@RequestMapping("config")
@Api(value = "config", description = "配置管理")
public class ConfigController {
	@Autowired
	private ConfigService configService;

	/**
	 * 配置列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("config:list")
	@ApiOperation(value = "配置列表信息", notes = "返回配置列表数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public R list(@ApiParam(value = "第几页", required = true) @RequestParam(required = false) Integer page,
			@ApiParam(value = "每页显示几条", required = true) @RequestParam(required = false) Integer limit) {
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);

		// 查询列表数据
		List<ConfigEntity> configList = configService.queryList(map);
		int total = configService.queryTotal(map);

		PageUtils pageUtil = new PageUtils(configList, total, limit, page);

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 信息
	 */
	@GetMapping("/info/{configId}")
	@RequiresPermissions("config:info")
	@ApiOperation(value = "根据id查询配置列表信息", notes = "根据id查询获取配置列表信息数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public R info(@ApiParam(value = "根据id查询获取对象", required = true) @PathVariable("configId") Integer configId) {
		ConfigEntity config = configService.queryObject(configId);

		return R.ok().put("config", config);
	}

	/**
	 * 保存
	 */
	@PostMapping("/save")
	@RequiresPermissions("config:save")
	@ApiOperation(value = "保存配置数据", notes = "保存配置数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public R save(@RequestBody ConfigEntity config) {
		configService.save(config);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@RequiresPermissions("config:update")
	@ApiOperation(value = "修改配置数据", notes = "修改配置数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public R update(@RequestBody ConfigEntity config) {
		configService.update(config);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	@RequiresPermissions("config:delete")
	@ApiOperation(value = "删除配置数据", notes = "删除配置数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public R delete(@RequestBody Integer[] configIds) {
		configService.deleteBatch(configIds);

		return R.ok();
	}

}
